﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using Silvermoon.UI;
using System.Linq;

namespace Testing
{
    public class PopupMenuDemo : DemoWindowBase
    {
        private Button button1, button2;
        private Label label;
        private PopupMenu contextMenu;

        public PopupMenuDemo()
        {
            SetTransition(500, TransitionMask.RotateLeft | TransitionMask.Right | TransitionMask.Fade, TransitionMask.RotateLeft | TransitionMask.Right | TransitionMask.Fade);

            Controls.Add(new GradientControl
                {
                    GradientColor = Color.DarkViolet,
                    TopColor = Color.Silver
                });


            contextMenu = new PopupMenu { Width = 300, Padding=new Margin(4) };
            contextMenu.Items.Add(new MenuItem { Text = "About...", Enabled = true });
            contextMenu.Items.Add(new MenuItem { Text = "Details...", Enabled=false });
            contextMenu.Items.Add(new MenuItem { Text = "Change Date..." });
            contextMenu.Items.Add(new MenuItem { Text = "Close" });

            contextMenu.ShowContextMenu += (s, e) =>
                {
                    bool flag = contextMenu.AnchorControl != button2;
                    contextMenu.Color = flag ? Color.Silver : Color.Black;
                    contextMenu.TextColor = flag ? Color.Black : Color.White;
                    Button b = (contextMenu.AnchorControl as Button);
                    if (contextMenu.Items.Count > 0)
                    {
                        (contextMenu.Items[0] as TextItem).Text = b != null ? (b.Content as StringShape).Text : "Label";
                    }
                };


            label = new Label
            {
                Margin = new Margin(24, null, 24, 4),
                Height = FontSprite.Regular.CharHeight * 2,
                LineMode = LineMode.WordWrap,
                LineAlignment = Alignment.Near,
                ContextMenu = contextMenu,
                Text = "Tap and Hold a button to open context menu."
            };
            button1 = new Button { Bounds = new Rectangle(64, 240, 240, 80), Content = "Show Menu 1", Color = Color.DarkBlue, ContextMenu = contextMenu };
            button2 = new Button { Bounds = new Rectangle(174, 240 + 124, 240, 80), Content = "Show Menu 2", Color = Color.Silver, ContextMenu = contextMenu, TextColor = Color.Black };
            Controls.Add(label, button1, button2);
        }

        protected override void Dispose(bool dispose)
        {
            if (contextMenu != null) contextMenu.Dispose();
            base.Dispose(dispose);
        }

        protected override void OnHiding()
        {
            if (contextMenu != null)
            {
                contextMenu.Hide();
                contextMenu.Visible = false;
            }
            base.OnHiding();
        }
    }
}
